<template>
  <div class="post-remark">
    <el-form :inline="true" :model="formData" :rules="rules" ref="formData">
      <el-form-item label="回复人" prop="userName">
        <el-input
          v-model="formData.userName"
          placeholder="回复人"
          autocomplete="off"
          :clearable="true"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="query">查询</el-button>
        <el-button type="info" @click="clear">清空</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <div class="admin-table">
      <div class="batch-btn">
        <el-button
          type="danger"
          size="mini"
          @click="batchDelete"
          v-show="this.multipleSelection.length > 0"
          >批量删除</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        v-loading="tableLoading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="46" align="center">
        </el-table-column>
        <el-table-column
          type="index"
          width="50"
          :index="indexConfig"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column
          label="回复人"
          prop="userName"
          width="200"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="回复内容"
          align="center"
          prop="content"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="回复时间" width="180" align="center">
          <template slot-scope="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="del(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        v-show="tableData.length > 0"
        background
        layout="prev, pager, next"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="total"
        @current-change="handleCurrentChange"
        :pager-count="7"
        style="text-align: right; margin-top: 24px"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getPostRemarkList,
  batchDeletePostRemark,
} from "@/api/admin/postRemark";
export default {
  name: "PostRemarkView",
  props: ["id"],
  data() {
    return {
      formData: {
        userName: "",
      },
      rules: {
        userName: [
          { required: true, message: "请输入回复人", trigger: "blur" },
        ],
      },
      page: 1,
      pageSize: 8,
      total: 0,
      currentPage: 1,
      // 表格
      tableData: [],
      tableLoading: false,
      multipleSelection: [],
    };
  },
  created() {
    this.getPostRemarkList();
  },
  methods: {
    // 查询
    query() {
      this.$refs.formData.validate((v) => {
        if (v) {
          this.page = 1;
          this.currentPage = 1;
          this.getPostRemarkList();
        }
      });
    },
    // 清空
    clear() {
      this.page = 1;
      this.currentPage = 1;
      this.$refs.formData.resetFields();
      this.getPostRemarkList();
    },
    // 获取帖子回复列表
    async getPostRemarkList() {
      const res = await getPostRemarkList({
        page: this.page,
        pageSize: this.pageSize,
        postId: this.id,
        ...this.formData,
      });
      this.tableData = res.data.records;
      this.total = res.data.total;
    },
    // 时间格式判断
    padZero(num) {
      return num < 10 ? "0" + num : num;
    },
    // 时间格式化
    formatDate(timeArray) {
      if (!timeArray) return "暂无时间";
      let year = timeArray[0];
      let month = this.padZero(timeArray[1]);
      let day = this.padZero(timeArray[2]);
      let hour = this.padZero(timeArray[3]);
      let minute = this.padZero(timeArray[4]);
      let second = this.padZero(timeArray[5]) || "00";
      return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    },
    // 配置表格索引
    indexConfig(index) {
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },
    // 当选项发生变化触发
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 分页查询
    handleCurrentChange(page) {
      this.page = page;
      this.currentPage = page;
      this.getPostRemarkList();
    },
    // 删除
    async del(row) {
      this.$confirm("确定删除所选数据吗？请谨慎操作！", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        try {
          this.tableLoading = true;
          const res = await batchDeletePostRemark([row.id]);
          await this.getPostRemarkList();
        } finally {
          this.tableLoading = false;
        }
      });
    },
    // 批量删除
    async batchDelete() {
      const ids = [];
      this.multipleSelection.forEach((item) => {
        ids.push(item.id);
      });
      this.$confirm("确定删除所选数据吗？请谨慎操作！", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        try {
          this.tableLoading = true;
          const res = await batchDeletePostRemark(ids);
          await this.getPostRemarkList();
        } finally {
          this.tableLoading = false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.post-remark {
  width: 100%;
  height: 100%;
  background-color: #fff;
  .admin-table {
    .batch-btn {
      height: 28px;
      margin-bottom: 4px;
    }
  }
}
/*
解决单选框样式问题
  Blocked aria-hidden on an element because its descendant retained focus. 
  The focus must not be hidden from assistive technology users. 
  Avoid using aria-hidden on a focused element or its ancestor. 
  Consider using the inert attribute instead, which will also prevent focus. 
  For more details, see the aria-hidden section of the WAI-ARIA specification at Element with focus: input Ancestor with aria-hidden:
*/
/deep/ input[aria-hidden="true"] {
  display: none !important;
}
/deep/.el-radio:focus:not(.is-focus):not(:active):not(.is-disabled)
  .el-radio__inner {
  box-shadow: none !important;
}
</style>